/**
 * Created by Spring on 2017/4/25.
 */
var wechat = (function ($, wx, config, urlFunc) {
    var helper = {
        getUrlParams: function () {
            if (window.location.href.indexOf('?') == -1) {
                return false;
            }
            var vars = {}, hash;
            var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
            for (var i = 0; i < hashes.length; i++) {
                hash = hashes[i].split('=');
                vars[hash[0]] = hash[1];
            }
            return vars;
        },
        getUrlParam: function (name) {
            return this.getUrlParams()[name];
        }
    }
    //登录完毕后执行
    var ready = function (callback) {
        var token = g.storage.getToken();
        if (!token || token == "undefined") {
            if (helper.getUrlParam("author")) {
                if (helper.getUrlParam("token")) {//之前登陆过直接生成token
                    g.storage.setToken(helper.getUrlParam("token"));
                } else {//之前没绑定过跳转到绑定登录页面
                    if (location.href.indexOf("/page/login") == -1) {
                        location.href = g.url.urlHref("/page/login.html?author=true&openid=" + helper.getUrlParam("openid") + "&nickname=" + helper.getUrlParam("nickname") + "&headerimg=" + helper.getUrlParam("headerimg"));
                    } else {
                        callback(token);
                    }
                }
            } else {
                location.href = config.wechatAuthor;
                return;

            }
        } else {
            callback(token);
        }

    }
    var sharedReady = function (callback) {
        if (window.wx) {
            callback();
        } else {
            console.log("未引入微信官方js文件")
        }
    }
    return {
        ready: ready,
        sharedReady: sharedReady
    }
})($, wx, g.config, g.url);